import sys
import os
sys.path.append(os.path.join(os.path.dirname(__file__), '..'))
from impl.db.database import DatabaseConnection
from impl.db.model.record import BaiduCrawlerRecordDAO
from impl.db.model.deduplication import BaiduCrawlerDeduplicationDAO
import traceback


def db_drop(db_config: dict):
    db = DatabaseConnection(db_config)
    print(f'Dropping tables : {db.table_names}')
    char = input('Please make sure! Type yes to continue: ')
    if char != 'yes':
        sys.exit()
    print('Dropping database...', end='')
    with db:
        record_dao = BaiduCrawlerRecordDAO(db)
        deduplication_dao = BaiduCrawlerDeduplicationDAO(db)
        try:
            record_dao.drop_table()
        except Exception:
            print(traceback.format_exc())
        try:
            deduplication_dao.drop_table()
        except Exception:
            print(traceback.format_exc())
    print('Done')


if __name__ == '__main__':
    from db_utils._get_db_config import get_db_config
    db_drop(get_db_config())
